import { defineStore } from 'pinia';
import { ref } from 'vue';
import { deepTree, revDeepTree, storage } from '/@/cool/utils';
import { isArray, isEmpty, orderBy } from 'lodash-es';
import { router, service } from '/@/cool';
import { revisePath } from '../utils';
import { config } from '/@/config';
import { Menu } from '/$/base/types';

// 本地缓存
const data = storage.info();

export const useMenuStore = defineStore('menu', function () {
	// 所有菜单
	const all = ref<Menu.List>([]);

	// 视图路由
	const routes = ref<Menu.List>([]);

	// 菜单组
	const group = ref<Menu.List>(data['base.menuGroup'] || []);

	// 左侧菜单列表
	const list = ref<Menu.List>([]);

	// 权限列表
	const perms = ref<any[]>(data['base.menuPerms'] || []);

	// 设置左侧菜单
	function setMenu(i: number = 0) {
		// 显示分组显示菜单
		if (config.app.menu.isGroup) {
			list.value = group.value.filter(e => e.isShow)[i]?.children || [];
		} else {
			list.value = group.value;
		}
	}

	// 设置权限
	function setPerms(list: Menu.List) {
		function deep(d: any) {
			if (typeof d == 'object') {
				if (d.permission) {
					if (d.namespace) {
						d._permission = {};
						for (const i in d.permission) {
							d._permission[i] =
								list.findIndex(e =>
									e
										.replace(/:/g, '/')
										.includes(`${d.namespace.replace('admin/', '')}/${i}`)
								) >= 0;
						}
					} else {
						console.error('namespace is required', d);
					}
				} else {
					for (const i in d) {
						deep(d[i]);
					}
				}
			}
		}

		perms.value = list;
		storage.set('base.menuPerms', list);

		deep(service);
	}

	// 设置视图
	function setRoutes(list: Menu.List) {
		// 获取第一个菜单路径
		const fp = getPath(group.value);

		// 查找符合路由
		const route = list.find(e => (e.meta!.isHome = e.path == fp));

		// 过滤菜单
		routes.value = list.filter(e => e.type == 1);

		if (route) {
			// 移除旧路由
			router.del('home');
			router.del('homeRedirect');

			// 添加一个重定向
			if (route.path != '/') {
				const item = routes.value.find(e => e.name == 'homeRedirect');

				if (item) {
					item.path = route.path;
				} else {
					routes.value.push({
						path: route.path,
						redirect: '/',
						name: 'homeRedirect'
					} as any);
				}
			}

			// 设置为首页
			route.path = '/';
			route.name = 'home';
		}
	}

	// 设置菜单组
	function setGroup(list: Menu.List) {
		group.value = orderBy(deepTree(list), 'orderNum');
		storage.set('base.menuGroup', group.value);
	}

	// 获取菜单，权限信息
	async function get() {
		function next(res: { menus: Menu.List; perms?: any[] }) {
			// 所有菜单
			all.value = res.menus;

			// 菜单格式化
			const list = res.menus
				?.filter(e => e.type != 2)
				.map(e => {
					const path = revisePath(e.router || String(e.id));
					const isShow = e.isShow === undefined ? true : e.isShow;

					return {
						...e,
						path,
						isShow,
						meta: {
							...e.meta,
							label: e.name, // 菜单名称的唯一标识
							keepAlive: e.keepAlive || 0
						},
						name: `${e.name}-${e.id}`, // 避免重复命名之前的冲突
						children: []
					};
				});

			// 设置权限
			setPerms(res.perms || []);

			// 设置菜单组
			setGroup(list);

			// 设置视图路由
			setRoutes(list);

			// 设置菜单
			setMenu();

			return list;
		}

		// 自定义菜单
		if (!isEmpty(config.app.menu.list)) {
			next({
				menus: revDeepTree(config.app.menu.list || [])
			});
		} else {
			// 动态菜单
			//await service.base.comm.permmenu().then(next);
			const menuList = [
				{
					"id": 42,
					"createTime": "2025-02-19 10:56:26",
					"updateTime": "2025-02-19 11:14:48",
					"tenantId": null,
					"parentId": null,
					"name": "首页",
					"router": "/",
					"perms": null,
					"type": 1,
					"icon": null,
					"orderNum": 0,
					"viewPath": "modules/demo/views/home/index.vue",
					"keepAlive": true,
					"isShow": false
				},
				{
					"id": 43,
					"createTime": "2025-02-19 10:56:26",
					"updateTime": "2025-02-19 10:56:26",
					"tenantId": null,
					"parentId": null,
					"name": "习题管理",
					"router": null,
					"perms": null,
					"type": 0,
					"icon": "icon-data",
					"orderNum": 7,
					"viewPath": null,
					"keepAlive": true,
					"isShow": true
				},
				{
					"id": 44,
					"createTime": "2025-02-19 10:56:26",
					"updateTime": "2025-02-19 10:56:26",
					"tenantId": null,
					"parentId": 43,
					"name": "习题列表",
					"router": "/question/crud",
					"perms": null,
					"type": 1,
					"icon": "icon-dict",
					"orderNum": 3,
					"viewPath": "modules/question/views/list.vue",
					"keepAlive": true,
					"isShow": true
				},
				{
					"id": 58,
					"createTime": "2025-02-19 10:56:27",
					"updateTime": "2025-02-19 10:56:27",
					"tenantId": null,
					"parentId": 43,
					"name": "习题审核",
					"router": "/question/audit",
					"perms": null,
					"type": 1,
					"icon": "icon-log",
					"orderNum": 5,
					"viewPath": "modules/audit/views/list.vue",
					"keepAlive": true,
					"isShow": true
				},
				{
					"id": 62,
					"createTime": "2025-02-19 10:56:27",
					"updateTime": "2025-02-19 10:56:27",
					"tenantId": null,
					"parentId": null,
					"name": "教材管理",
					"router": null,
					"perms": null,
					"type": 0,
					"icon": "icon-menu",
					"orderNum": 1,
					"viewPath": null,
					"keepAlive": false,
					"isShow": true
				},
				{
					"id": 63,
					"createTime": "2025-02-19 10:56:27",
					"updateTime": "2025-02-19 10:56:27",
					"tenantId": null,
					"parentId": 62,
					"name": "教材列表",
					"router": "/textbook/book",
					"perms": null,
					"type": 1,
					"icon": "icon-menu",
					"orderNum": 1,
					"viewPath": "modules/textbook/views/book.vue",
					"keepAlive": true,
					"isShow": true
				},
				{
					"id": 64,
					"createTime": "2025-02-19 10:56:27",
					"updateTime": "2025-02-19 10:56:27",
					"tenantId": null,
					"parentId": 62,
					"name": "教材操作",
					"router": "/textbook/detail",
					"perms": null,
					"type": 1,
					"icon": "icon-menu",
					"orderNum": 1,
					"viewPath": "modules/textbook/views/detail.vue",
					"keepAlive": true,
					"isShow": true
				},
				{
					"id": 37,
					"createTime": "2025-02-19 10:56:26",
					"updateTime": "2025-02-19 10:56:26",
					"tenantId": null,
					"parentId": null,
					"name": "课程管理",
					"router": null,
					"perms": null,
					"type": 0,
					"icon": "icon-favor",
					"orderNum": 1,
					"viewPath": null,
					"keepAlive": false,
					"isShow": true
				},
				{
					"id": 38,
					"createTime": "2025-02-19 10:56:26",
					"updateTime": "2025-02-19 10:56:26",
					"tenantId": null,
					"parentId": 37,
					"name": "课程列表",
					"router": "/course/list",
					"perms": null,
					"type": 1,
					"icon": "icon-favor",
					"orderNum": 1,
					"viewPath": "modules/course/views/index.vue",
					"keepAlive": true,
					"isShow": true
				},
				{
					"id": 39,
					"createTime": "2025-02-19 10:56:26",
					"updateTime": "2025-02-19 10:56:26",
					"tenantId": null,
					"parentId": 37,
					"name": "课程操作",
					"router": "/course/detail",
					"perms": null,
					"type": 1,
					"icon": "icon-favor",
					"orderNum": 1,
					"viewPath": "modules/course/views/detail.vue",
					"keepAlive": true,
					"isShow": true
				},
				{
					"id": 68,
					"createTime": "2025-02-19 10:56:27",
					"updateTime": "2025-02-19 10:56:27",
					"tenantId": null,
					"parentId": null,
					"name": "知识点管理",
					"router": null,
					"perms": null,
					"type": 0,
					"icon": "icon-auth",
					"orderNum": 1,
					"viewPath": null,
					"keepAlive": false,
					"isShow": true
				},

				{
					"id": 69,
					"createTime": "2025-02-19 10:56:27",
					"updateTime": "2025-02-19 10:56:27",
					"tenantId": null,
					"parentId": 68,
					"name": "知识点列表",
					"router": "/knowledge/crud",
					"perms": null,
					"type": 1,
					"icon": "icon-list",
					"orderNum": 1,
					"viewPath": "modules/knowledge/views/index.vue",
					"keepAlive": true,
					"isShow": true
				},
				{
					"id": 69,
					"createTime": "2025-02-19 10:56:27",
					"updateTime": "2025-02-19 10:56:27",
					"tenantId": null,
					"parentId": 68,
					"name": "知识关联图谱",
					"router": "/knowledge/graph",
					"perms": null,
					"type": 1,
					"icon": "icon-list",
					"orderNum": 2,
					"viewPath": "modules/knowledge/views/graph.vue",
					"keepAlive": true,
					"isShow": true
				},
				/*{
					"id": 101,
					"createTime": "2025-02-19 10:56:26",
					"updateTime": "2025-02-19 10:56:26",
					"tenantId": null,
					"parentId": null,
					"name": "系统管理",
					"router": "/sys",
					"perms": null,
					"type": 0,
					"icon": "icon-set",
					"orderNum": 21,
					"viewPath": null,
					"keepAlive": true,
					"isShow": true
				},
				{
					"id": 102,
					"createTime": "2025-02-19 10:56:26",
					"updateTime": "2025-02-19 10:56:26",
					"tenantId": null,
					"parentId": 101,
					"name": "权限管理",
					"router": null,
					"perms": null,
					"type": 0,
					"icon": "icon-auth",
					"orderNum": 1,
					"viewPath": null,
					"keepAlive": false,
					"isShow": true
				},
				{
					"id": 104,
					"createTime": "2025-02-19 10:56:26",
					"updateTime": "2025-02-19 10:56:26",
					"tenantId": null,
					"parentId": 102,
					"name": "用户列表",
					"router": "/sys/user",
					"perms": null,
					"type": 1,
					"icon": "icon-user",
					"orderNum": 0,
					"viewPath": "modules/base/views/user/index.vue",
					"keepAlive": true,
					"isShow": true
				},
				{
					"id": 105,
					"createTime": "2025-02-19 10:56:26",
					"updateTime": "2025-02-19 10:56:26",
					"tenantId": null,
					"parentId": 102,
					"name": "菜单列表",
					"router": "/sys/menu",
					"perms": null,
					"type": 1,
					"icon": "icon-menu",
					"orderNum": 2,
					"viewPath": "modules/base/views/menu/index.vue",
					"keepAlive": true,
					"isShow": true
				},
				{
					"id": 106,
					"createTime": "2025-02-19 10:56:26",
					"updateTime": "2025-02-19 10:56:26",
					"tenantId": null,
					"parentId": 102,
					"name": "角色列表",
					"router": "/sys/role",
					"perms": null,
					"type": 1,
					"icon": "icon-dept",
					"orderNum": 3,
					"viewPath": "cool/modules/base/views/role.vue",
					"keepAlive": true,
					"isShow": true
				},*/
			];
			next({ menus: menuList as Menu.Item[], perms: []})
		}
	}

	// 获取菜单路径
	function getPath(data: Menu.Item | Menu.List) {
		const list = isArray(data) ? data : [data];

		let path = '';

		function deep(arr: Menu.List) {
			arr.forEach((e: Menu.Item) => {
				switch (e.type) {
					case 0:
						deep(e.children || []);
						break;
					case 1:
						if (!path) {
							path = e.path;
						}
						break;
				}
			});
		}

		deep(list);

		return path;
	}

	return {
		all,
		routes,
		group,
		list,
		perms,
		get,
		setPerms,
		setMenu,
		setRoutes,
		setGroup,
		getPath
	};
});
